package net.dlz.example.controller;



import net.dlz.example.domain.Policy;
import net.dlz.example.domain.SearchPolicyResponse;
import net.dlz.example.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/Policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    @RequestMapping("/list")
    public String PolicyList(Model model){
        List<Policy> policys =policyService.listPolicy();
        model.addAttribute("policys",policys);
        return "policy_list";}

    @RequestMapping("/alist")
    public String PolicyAdminList(){return "policyadmin_list";}

    @GetMapping("/api/search")
    @ResponseBody
    public SearchPolicyResponse searchPolicyResponse(@RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize){
        return policyService.findAllPolicy(pageNum,pageSize);
    }

    @GetMapping("/api/searchLink")
    @ResponseBody
    public SearchPolicyResponse searchPolicyResponseLink(@RequestParam("keywords") String keywords,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize){
        return policyService.findPolicyLinkTopic(keywords,pageNum,pageSize);
    }
    @RequestMapping("/toadd")
    public String addPolicyPage(Model model){
        model.addAttribute("policy",new Policy());
        return "policy_submit";
    }
    @RequestMapping("/add")
    public String addPolicy(Policy policy){
        int updated = policyService.savePolicy(policy);
        return "redirect:/Policy/list";
    }




    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deletePolicy(@RequestParam("id") String id){
        int deleted = policyService.deletePolicy(id);
        return deleted > 0;
    }
    @RequestMapping("/content")
    public String PolicyContent(@RequestParam("id") String id,Model model){
        Policy policy = policyService.findPolicyByPrimaryKey(id);
        model.addAttribute("policy",policy);
        return "policy_content";
    }

}
